﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Reflection;

namespace luanvanFinal
{
    /// <summary>
    /// Interaction logic for wdShowVersion.xaml
    /// </summary>
    public partial class wdShowVersion : Window
    {
        private int idProject;
        public wdShowVersion()
        {
            InitializeComponent();
        }
        public int IdProject
        {
            get { return idProject; }
            set { idProject = value; }
        }
        public void showUser()
        {
            String SQL = "select idReq from version where idProject = '" + idProject + "'and class like 'User%' ";
            clsVersion version = new clsVersion();
            DataTable dt = version.getDataVersion(SQL);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                clsItemsVersion ver = new clsItemsVersion();
                ver.IdReq = dt.Rows[i][0].ToString();
                ver.Width=100;
                ver.Height = 100;                
                ver.Background = Brushes.LemonChiffon;
                ver.SetValue(Canvas.LeftProperty, 160.0 + i * 150.0);
                ver.SetValue(Canvas.TopProperty, 50.0);                
                String SQLReqTitle = "select reqTitle from req where idReq ='"+dt.Rows[i][0].ToString()+"'";
                DataTable dataReqTitle = clsReq.getDataProject(SQLReqTitle);
                String SQLReqNote = "select note from line where clsUser ='" + dt.Rows[i][0].ToString() + "'";
                DataTable dataNote = clsLine.getDataLine(SQLReqNote);
                String reqNote = "******************************";
                for (int n = 0; n < dataNote.Rows.Count; n++)
                {
                    reqNote = reqNote + Environment.NewLine + "----------------------------" + Environment.NewLine + dataNote.Rows[n][0].ToString();
                }
                ver.Note = reqNote;
                ver.ReqTitle = dataReqTitle.Rows[0][0].ToString();
                cvs.Children.Add(ver);
                Label label = new Label();
                label.Content = dt.Rows[i][0];
                label.SetValue(Canvas.LeftProperty, 165.0 + i * 150.0);
                label.SetValue(Canvas.TopProperty, 70.0);                
                label.FontSize = 15;
                cvs.Children.Add(label);
                Label labelReqTitle = new Label();
                labelReqTitle.Content = dataReqTitle.Rows[0][0];
                labelReqTitle.SetValue(Canvas.LeftProperty, 165.0 + i * 150.0);
                labelReqTitle.SetValue(Canvas.TopProperty, 90.0);
                labelReqTitle.FontSize = 15;
                cvs.Children.Add(labelReqTitle);
                ver.Note = reqNote;
                ver.Click += a_Click;
            }
        }

        public void showSys()
        {
            String SQL = "select idReq from version where idProject = '" + idProject + "'and class like 'Sys%' ";
            clsVersion version = new clsVersion();
            DataTable dt = version.getDataVersion(SQL);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                clsItemsVersion ver = new clsItemsVersion();
                ver.IdReq = dt.Rows[i][0].ToString();
                ver.Width = 100;
                ver.Height = 100;
                ver.Background = Brushes.LightGreen;
                ver.SetValue(Canvas.LeftProperty, 160.0 + i * 150.0);
                ver.SetValue(Canvas.TopProperty, 210.0);
                String SQLReqTitle = "select reqTitle from req where idReq ='" + dt.Rows[i][0].ToString() + "'";
                DataTable dataReqTitle = clsReq.getDataProject(SQLReqTitle);
                String SQLReqNote = "select note from line where clsSystem ='" + dt.Rows[i][0].ToString() + "'";
                DataTable dataNote = clsLine.getDataLine(SQLReqNote);
                String reqNote = "*************************";
                for (int n = 0; n < dataNote.Rows.Count; n++)
                {
                    reqNote = reqNote + Environment.NewLine + "----------------------------" + Environment.NewLine + dataNote.Rows[n][0].ToString();
                }
                ver.Note = reqNote;
                ver.ReqTitle = dataReqTitle.Rows[0][0].ToString();
                cvs.Children.Add(ver);
                Label label = new Label();
                label.Content = dt.Rows[i][0];
                label.SetValue(Canvas.LeftProperty, 165.0 + i * 150.0);
                label.SetValue(Canvas.TopProperty, 230.0);
                label.FontSize = 15;
                cvs.Children.Add(label);
                Label labelReqTitle = new Label();
                labelReqTitle.Content = dataReqTitle.Rows[0][0];
                labelReqTitle.SetValue(Canvas.LeftProperty, 165.0 + i * 150.0);
                labelReqTitle.SetValue(Canvas.TopProperty, 250.0);
                labelReqTitle.FontSize = 15;
                cvs.Children.Add(labelReqTitle);
                ver.Click += a_Click;
            }
        }

        public void showDesign()
        {
            String SQL = "select idReq from version where idProject = '" + idProject + "'and class like 'design%' ";
            clsVersion version = new clsVersion();
            DataTable dt = version.getDataVersion(SQL);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                clsItemsVersion ver = new clsItemsVersion();
                ver.IdReq = dt.Rows[i][0].ToString();
                ver.Width = 100;
                ver.Height = 100;
                ver.Background = Brushes.LightCyan;
                ver.SetValue(Canvas.LeftProperty, 160.0 + i * 150.0);
                ver.SetValue(Canvas.TopProperty, 370.0);
                String SQLReqTitle = "select reqTitle from req where idReq ='" + dt.Rows[i][0].ToString() + "'";
                DataTable dataReqTitle = clsReq.getDataProject(SQLReqTitle);
                String SQLReqNote = "select note from line where clsDesign ='" + dt.Rows[i][0].ToString() + "'";
                DataTable dataNote = clsLine.getDataLine(SQLReqNote);
                String reqNote = "**************************";
                for (int n = 0; n < dataNote.Rows.Count; n++)
                {
                    reqNote = reqNote + Environment.NewLine + "----------------------------" + Environment.NewLine + dataNote.Rows[n][0].ToString();
                }
                ver.Note = reqNote;
                ver.ReqTitle = dataReqTitle.Rows[0][0].ToString();
                cvs.Children.Add(ver);
                Label label = new Label();
                label.Content = dt.Rows[i][0];
                label.SetValue(Canvas.LeftProperty, 165.0 + i * 150.0);
                label.SetValue(Canvas.TopProperty, 390.0);
                label.FontSize = 15;
                cvs.Children.Add(label);
                Label labelReqTitle = new Label();
                labelReqTitle.Content = dataReqTitle.Rows[0][0];
                labelReqTitle.SetValue(Canvas.LeftProperty, 165.0 + i * 150.0);
                labelReqTitle.SetValue(Canvas.TopProperty, 410.0);
                labelReqTitle.FontSize = 15;
                cvs.Children.Add(labelReqTitle);
                ver.Click += a_Click;
            }
        }
       
        public void drawline()
        {
            
            String userReq;
            String sysReq;
            String desReq;
            List < Brush> myBrushes = new List<Brush> ();
            myBrushes.Add(Brushes.Black);
            myBrushes.Add(Brushes.Aqua);
            myBrushes.Add(Brushes.LightGreen);
            myBrushes.Add(Brushes.DarkOrange);
            myBrushes.Add(Brushes.DarkRed);
            clsLine line = new clsLine();            
            DataTable dt = line.getAllDataVersion(idProject);
            clsVersion version = new clsVersion();
            String SQLUser = "select * from version where idProject = '"+idProject+"' and class like 'User%'";
            String SQLSys = "select * from version where idProject =  '" + idProject + "' and class like 'Sys%'";
            String SQLDes = "select * from version where idProject =  '" + idProject + "' and class like 'Design%'";
            DataTable verUser = version.getDataVersion(SQLUser);
            DataTable verSys = version.getDataVersion(SQLSys);
            DataTable verDes = version.getDataVersion(SQLDes);
            int[,] index = new int[dt.Rows.Count,3];
            for(int i=0;i<dt.Rows.Count;i++)
            {                  
                userReq = dt.Rows[i][2].ToString();
                sysReq = dt.Rows[i][3].ToString();
                desReq = dt.Rows[i][4].ToString();
                for (int a = 0; a < verUser.Rows.Count; a++)
                {
                    if (verUser.Rows[a][2].ToString() == userReq)
                    {
                        index[i,0] = a + 1 ;
                    }
                }
                for (int a = 0; a < verSys.Rows.Count; a++)
                {
                    if (verSys.Rows[a][2].ToString() == sysReq)
                    {
                        index[i,1] = a + 1;
                    }
                }
                for (int a = 0; a < verDes.Rows.Count; a++)
                {
                    if (verDes.Rows[a][2].ToString() == desReq)
                    {
                        index[i,2] = a + 1;
                    }
                }
               
                
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                    Point[] point = new Point[3];
                    point[0].X = 210.0+ 1.5*i + (index[i,0]-1) * 150.0;
                    point[0].Y = 130;
                    point[1].X = 210.0 + 1.5 * i + (index[i, 1] - 1) * 150.0;
                    point[1].Y = 260;
                    point[2].X = 210.0 + 1.5 * i + (index[i, 2] - 1) * 150.0;
                    point[2].Y = 450;
                    Polyline polyLine = new Polyline();
                    polyLine.Stroke = myBrushes[i%6];
                    polyLine.Points.Add(point[0]);
                    polyLine.Points.Add(point[1]);
                    polyLine.Points.Add(point[2]);
                    polyLine.StrokeThickness = 6;
                    cvs.Children.Add(polyLine);                
            }
        }
        private void a_Click(object sender, EventArgs e)
        {
            clsItemsVersion ab = sender as clsItemsVersion;
            wdShowNote showNote = new wdShowNote();
            showNote.Note = ab.Note;
            showNote.Show();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            drawline();
            showUser();
            showSys();
            showDesign();            
        }
    }
}
